package day01;

import java.sql.*;

public class TestJDBC1 {

    private static String driverClassName="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf8";
    private static String username="root";
    private static String password="123";


    public static void main(String[] args) throws Exception {

//        //1.注册驱动
////        Driver driver=new com.mysql.jdbc.Driver();
////        DriverManager.registerDriver(driver);
//
//        Class.forName(driverClassName);
//
//
//        //2.建立连接
//        Connection con=DriverManager.getConnection(url,username,password);
//
//        System.out.println(con);
//
//
//        //3.获得操作对象
//        Statement stat=con.createStatement();
//
//        System.out.println(stat);
//
//        //4.发送sql语句
//        String sql=new StringBuffer()
//                .append("create table t_user( ")
//                .append("    id int primary key auto_increment, ")
//                .append("    name varchar(20) ")
//                .append(") ")
//                .toString();
//
//        System.out.println(sql);
//
//        stat.execute(sql);
//
//        //5.释放资源
//        stat.close();
//        con.close();



//        create();

//        insert();


//        update();


//        delete();


//        select();


//        selectById(3);


//        selectByName("ccc");

        selectByName("sadfsadfasdf' or '1'='1");


    }




    public static void create(){


        Connection con=null;
        Statement stat=null;
        try{

            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            stat=con.createStatement();

            String sql=new StringBuffer()
                .append("create table t_user( ")
                .append("    id int primary key auto_increment, ")
                .append("    name varchar(20) ")
                .append(") ")
                .toString();

            stat.execute(sql);

        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(stat!=null){
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }




    }


    public static void insert(){
        Connection con=null;
        Statement stat=null;

        try{

            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            stat=con.createStatement();

            String sql=new StringBuffer()
                    .append("insert into t_user ")
                    .append("    (name) ")
                    .append("values ")
                    .append("    ('ddd') ")
                    .toString();

            stat.executeUpdate(sql);


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(stat!=null){
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void update(){

        Connection con=null;
        Statement stat=null;

        try{

            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            stat=con.createStatement();

            String sql=new StringBuffer()
                    .append("update t_user ")
                    .append("set name='abc' ")
                    .append("where id=1 ")
                    .toString();

            stat.executeUpdate(sql);



        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(stat!=null){
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    public static void delete(){
        Connection con=null;
        Statement stat=null;

        try{

            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            stat=con.createStatement();

            String sql=new StringBuffer()
                    .append("delete from t_user ")
                    .append("where id=1 ")
                    .toString();

            stat.executeUpdate(sql);


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(stat!=null){
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void select(){
        Connection con=null;
        Statement stat=null;
        ResultSet rs=null;

        try{

            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            stat=con.createStatement();

            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_user ")
                    .toString();

            rs=stat.executeQuery(sql);

            while(rs.next()){
//                int id=rs.getInt(1);
//                String name=rs.getString(2);

                int id=rs.getInt("id");
                String name=rs.getString("name");

                System.out.println(id+"\t"+name);

            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null){
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void selectById(int id){
        Connection con=null;
        Statement stat=null;
        ResultSet rs=null;

        try{

            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            stat=con.createStatement();

            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_user ")
                    .append("where id="+id)
                    .toString();

            rs=stat.executeQuery(sql);

            if(rs.next()){
                id=rs.getInt("id");
                String name=rs.getString("name");

                System.out.println(id);
                System.out.println(name);
            }



        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null){
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void selectByName(String name){
        Connection con=null;
        Statement stat=null;
        ResultSet rs=null;

        try{

            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            stat=con.createStatement();

            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_user ")
                    .append("where name='"+name+"' ")
                    .toString();

            System.out.println(sql);

            rs=stat.executeQuery(sql);

            while(rs.next()){
                System.out.println(rs.getInt("id")+"\t"+rs.getString("name"));
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null){
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

























































}
